*==========================================================================================================================================================
*
*	Local Projections
*
*==========================================================================================================================================================


set more off

qui {


*-------------------------------------------------------------------------------------------------------------
*	SETTINGS
*-------------------------------------------------------------------------------------------------------------

* Variables
local vars "wool_prod wool_prod2 tol_can tol_clo tol_foo ships_bas" 

* irf horizon
local H 5
local F 0

* rhs controls lags
local L 2

*-------------------------------------------------------------------------------------------------------------



*_________ GET DATA ____________
use "data/working files/prepared.dta", clear /*read in data*/

// timing: flocksize counted at beginning of year; wool washed in summer before usual fleet arrival; but washed wool data = 2.5*flocksize by definition; so washed wool data also is beginning of year data (no actual washed wool weighed)
// culling season: Mar, i.e. Jan flocksize is pre-culling + pre-disaster; next year Jan data is post-disaster but pre-culling => explains lack of on-impact response of flocksize
replace wool_prod_gr = wool_prod_gr[_n+1]
forvalues h = 0/`H' {
	replace wool_prod_gr`h' = wool_prod_gr`h'[_n+1]
}



*____ DECLARE TIME SERIES _____
gen id =1
tsset id year, yearly


*_________________ LOCAL PROJECTIONS _________________

local count = 0
foreach v of local vars {
local count = `count'+1
n di "Variable `v'"

***_________ CONTROLS

local indep	" cpi_sil_gr wage_sil_gr itsN_gr" 

forvalues i=0/`L'{
	local indep_`i' = subinstr("`indep'"," "," l`i'_",.)
	}
	
local rhs ""
forvalues i = 1/`L'{
	local rhs "`rhs'`indep_`i''"
	}	
	
	local not l0_`v'_gr l0_itsN_gr 
	local rhs: list rhs- not



	***_________ IMPULSE RESPONSE and CONFIDENCE INTERVAL variables
	gen irf_`v' = . if _n<=`H'+1
	
	gen se_`v' = . if _n<=`H'+1

	gen sig_`v' = . if _n<=`H'+1
	gen ci_up_`v' = . if _n<=`H'+1
	gen ci_lo_`v' = . if _n<=`H'+1
	gen ci_up90_`v' = . if _n<=`H'+1
	gen ci_lo90_`v' = . if _n<=`H'+1
			

	
	
	***_________ ESTIMATE COEFFICIENTS
	
	forvalues y = 0/`H' {
		n display "projection `y'"
		
		sum `v' 
		if (`r(N)' < 200) {
			local F = 0
			local LS = 1
			newey `v'_gr`y' l(1/`LS').`v'_gr l(-`F'/`LS').percloss_netN, lag(5) force
		}
		sum `v'
		if (`r(N)' >= 200) {
			local F = 5
			local LS = 2
			newey `v'_gr`y' l(1/`LS').`v'_gr l(-`F'/`LS').perclossN_vellon l(-`F'/`LS').perclossN l(-`F'/`LS').percloss_salvN `rhs' l(-`F'/`LS').percloss_pirN l(0/`LS').temperature l(0/`LS').warbeu l(0/`LS').warbciv, lag(5) 
		}


		* store IMPULSE RESPONSES and STANDARD ERRORS	
		sum `v' 
		if (`r(N)' < 200) {
		lincom percloss_netN
		}
		else {
		lincom perclossN 
		}
		
		replace irf_`v' = r(estimate) if _n==`y'+1 /* coefficients */
		replace se_`v' = r(se) if _n==`y'+1 /* standard errors */
		

		} //horizon			
	} // vars


	
local count = 0
foreach v of local vars {
local count = `count'+1
n di "Variable `v'"

	***_________ CONFIDENCE INTERVALS
	replace ci_up_`v' = irf_`v' + 1*se_`v' if _n <= `H'+1
	replace ci_lo_`v' = irf_`v' - 1*se_`v' if _n <= `H'+1
	replace ci_up90_`v' = irf_`v' + 1.64*se_`v' if _n <= `H'+1
	replace ci_lo90_`v' = irf_`v' - 1.64*se_`v' if _n <= `H'+1
	
	replace sig_`v' =  irf_`v' if ci_up90_`v' < 0 | ci_lo90_`v' > 0

}

* plot prerequisites
gen time = _n-1 if(_n<=`H'+1) 
gen time2 = time + 0.1 

drop if time > `H'
keep time time* irf_* ci_up_* ci_lo_*  ci_up90_* ci_lo90_* se_* sig_*
order irf_* ci_up_* ci_lo_*  time ci_up90_* ci_lo90_* sig_*
drop if time == . /*so quarters uniquely identifies each observation*/
gen zero = 0 if time!=.

local lsize "3"
local tsize "3.5"
local legsize "3"


	
//Primary output
twoway  (line zero time, lcolor(black) lwidth(vthin)) ///
		(scatter irf_tol_foo time, color(black) msymbol(circle_hollow)) ///
		(line irf_tol_foo time, lcolor(black) lpattern(dot) lwidth(medium)) ///
		(scatter irf_wool_prod time, color(gs8) msymbol(diamond_hollow)) ///
		(line irf_wool_prod time, lcolor(gs8) lpattern(dot) lwidth(medium)) ///
		(scatter sig_tol_foo time, color(black) msymbol(circle)) ///
		(scatter sig_wool_prod time, color(gs8) msymbol(diamond)), ///
		title("Primary sector", size(`tsize')) xtitle("Year", size(`lsize')) ytitle("Percent", height(2) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(-8(2)3, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(size(`legsize') symxsize(*.5) order(2 4) col(1) pos(5) ring(0) lab(2 "Foodstuffs") lab(4 "Flock size/Wool") region(color(white%0))) ///
		graphregion(color(white)) plotregion(color(white)) name(g1, replace) nodraw

	
//Textile output
twoway  (line zero time, lcolor(black) lwidth(vthin)) ///
		(scatter irf_wool_prod2 time, color(black) msymbol(circle_hollow)) ///
		(line irf_wool_prod2 time, lcolor(black) lpattern(dot) lwidth(medium)) ///
		(scatter sig_wool_prod2 time, color(black) msymbol(circle)), ///
		title("Textile production", size(`tsize')) xtitle("Year", size(`lsize')) ytitle("Percent", height(2) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(-4(2)3, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(off) ///
		graphregion(color(white)) plotregion(color(white)) name(g2, replace) nodraw
		*/

		
//Shipbuilding
twoway  (line zero time, lcolor(black) lwidth(vthin)) ///
		(scatter irf_ships_bas time, color(black) msymbol(circle_hollow)) ///
		(line irf_ships_bas time, lcolor(black) lpattern(dot) lwidth(medium)) ///
		(scatter sig_ships_bas time, color(black) msymbol(circle)), ///
		title("Ship production", size(`tsize')) xtitle("Year", size(`lsize')) ytitle("Percent", height(2) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(-2(1)2, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(off) ///
		graphregion(color(white)) plotregion(color(white)) name(g3, replace) nodraw
		
		
//Textile consumption and transit trade
twoway  (line zero time, lcolor(black) lwidth(vthin)) ///
		(scatter irf_tol_can time, color(black) msymbol(circle_hollow)) ///
		(line irf_tol_can time, lcolor(black) lpattern(dot) lwidth(medium)) ///
		(scatter irf_tol_clo time2, color(gs8) msymbol(diamond_hollow)) ///
		(line irf_tol_clo time, lcolor(gs8) lpattern(dot) lwidth(medium)) ///
		(scatter sig_tol_can time, color(black) msymbol(circle)) ///
		(scatter sig_tol_clo time2, color(gs8) msymbol(diamond)), ///
		title("Textile consumption/transit", size(`tsize')) xtitle("Year", size(`lsize')) ytitle("Percent", height(2) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(-7(2)3, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(size(`legsize') symxsize(*.5) order(2 4) col(1) pos(7) ring(0) lab(2 "Coarse cloth") lab(4 "Fine cloth") region(color(white%0))) ///
		graphregion(color(white)) plotregion(color(white)) name(g4, replace) nodraw
		
	
graph combine g1 g2 g4 g3, name(LP_disag_output, replace) cols(2) imargin(medlarge) plotregion(color(white)) graphregion(color(white)) 
graph display LP_disag_output, ysize(17) xsize(20) 
graph export "results/Figure3.pdf", replace 	



} // qui


*_________________________________________

			

			
